<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/23 11:24
 * Modified Time: 2019/7/23 11:24
 * Version: 2.0
 */

namespace App\Lib\File;

//阿里云
use App\Exceptions\AllValidatorException;
use OSS\OssClient;
use OSS\Core\OssException;

class AliOSS
{
    private $accessKeyId;
    private $accessKeySecret;
    private $endpoint;
    private $bucket;
    private $object;

    public function __construct()
    {
        $this->accessKeyId = config('constant.aliOss.access_key_id');
        $this->accessKeySecret = config('constant.aliOss.access_key_secret');
        $this->endpoint = config('constant.aliOss.endpoint');
        $this->bucket = config('constant.aliOss.bucket');
    }

    /*
     * 上传文件
     * $file:$request->file('file')
     * $oss_path:上传路径
     */
    public function upload($file, $oss_path)
    {
        $originalName = $file->getClientOriginalName(); // 文件原名
        $ext = $file->getClientOriginalExtension();     // 扩展名
        $realPath = $file->getRealPath();   //临时文件的绝对路径
        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;
        $bucket = $this->bucket;
        $file_name = md5($originalName . time()) . '.' . $ext;

        $object = $oss_path . "/" . $file_name;
        $filePath = $realPath;

        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $ossClient->uploadFile($bucket, $object, $filePath);

            $ossName = 'https://' . $bucket . '.oss-cn-qingdao.aliyuncs.com/' . $oss_path . '/' . $file_name;

            return ['status' => 1, 'ossName' => $ossName];
        } catch (OssException $e) {
//            printf(__FUNCTION__ . ": FAILED\n");
//            printf($e->getCode() . "\n".$e->getMessage() . "\n");
            throw new AllValidatorException(['msg' => $e->getMessage(), 'statusCode' => 400, 'errorCode' => 400]);

        }
    }

    //根据路径上传
    public function uploadByPath($file_path, $oss_path,$file_name,$file_ext,$is_md5=true)
    {
        $originalName = $file_name; // 文件原名
        $ext = $file_ext;     // 扩展名
        $realPath = $file_path;   //临时文件的绝对路径
        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;
        $bucket = $this->bucket;
        if($is_md5){
            $file_name = md5($originalName . time()) . '.' . $ext;
        }else{
            $file_name = $originalName  . '.' . $ext;
        }


        $object = $oss_path . "/" . $file_name;
        $filePath = $realPath;
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $ossClient->uploadFile($bucket, $object, $filePath);

            $ossName = 'https://' . $bucket . '.oss-cn-qingdao.aliyuncs.com/' . $oss_path . '/' . $file_name;

            return ['status' => 1, 'ossName' => $ossName];
        } catch (OssException $e) {
//            printf(__FUNCTION__ . ": FAILED\n");
//            printf($e->getCode() . "\n".$e->getMessage() . "\n");
            throw new AllValidatorException(['msg' => $e->getMessage(), 'statusCode' => 400, 'errorCode' => 400]);

        }
    }

    /*
     * $object:文件路径
     */
    public function checkFileExist($object)
    {
        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;
        $bucket = $this->bucket;

        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $exist = $ossClient->doesObjectExist($bucket, $object);
            return ['status' => 1, 'exist' => $exist];
        } catch (OssException $e) {
//            printf(__FUNCTION__ . ": FAILED\n");
//            printf($e->getMessage() . "\n");
            throw new AllValidatorException(['msg' => $e->getMessage(), 'statusCode' => 400, 'errorCode' => 400]);

        }
        print(__FUNCTION__ . ": OK" . "\n");
    }

    /*
     * 删除文件
     */
    public function delete($object)
    {
        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $endpoint = $this->endpoint;
        $bucket = $this->bucket;

        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

            $ossClient->deleteObject($bucket, $object);
        } catch (OssException $e) {
//            printf(__FUNCTION__ . ": FAILED\n");
//            printf($e->getMessage() . "\n");
            throw new AllValidatorException(['msg' => $e->getMessage(), 'statusCode' => 400, 'errorCode' => 400]);

        }
    }
}